<template>
	<view>
		<view class="pannel">
			<view class="avatar">
				<u-avatar :src="src" size="160"></u-avatar>
			</view>
			<u-form :model="form" ref="uForm" label-width="160">
				<u-form-item label="昵称" prop="userName"><u-input v-model="form.userName" :disabled="disabled" /></u-form-item>
				<u-form-item label="性别">
					<u-radio-group v-model="radio">
						<u-radio v-for="(item, index) in radioList" :key="index" :name="item.name"
							:disabled="item.disabled">
							{{ item.name }}
						</u-radio>
					</u-radio-group>
				</u-form-item>
				<u-form-item label="用户名"><u-input v-model="form.userId" :disabled="true" /></u-form-item>
				<u-form-item label="联系电话" prop="phonenumber"><u-input v-model="form.phonenumber" :disabled="disabled" /></u-form-item>
				<u-form-item label="身份证"><u-input v-model="form.idCard" :disabled="true" /></u-form-item>
				<u-form-item label="邮箱"><u-input v-model="form.email" :disabled="true" /></u-form-item>
			</u-form>
			<view class="bottom">
				<button @click="change">修改信息</button>
				<button @click="submit">确定</button>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				src: '',
				disabled: true,
				radio: '男',
				radioList: [
					{
						name: '男',
						disabled: true
					},
					{
						name: '女',
						disabled: true
					}
				],
				rules: {
					userName: [
						{
							required: true,
							message: '请输入姓名',
							trigger: ['blur','change']
							}
					],
					phoneNumber: [
						{
							required: true,
							message: '手机号不能为空',
							trigger: ['blur','change']
						},
						{
							min: 11,
							max: 11,
							message: '格式错误',
							trigger: 'change'
						}
					]
				},
				user: () => {},
				flag: false,
				form: {
					userName: '',
					sex: '',
					userId: '',
					phonenumber: '',
					idCard: '',
					email: ''
				}
			};
		},
		onLoad() {
			this.getData()
		},
		methods: {
			change(){
				this.disabled = false
				this.radioList.forEach(item => item.disabled = false)
			},
			submit() {
				this.$refs.uForm.validate(valid => {
					if(valid){
						console.log('验证通过')
						uni.request({
							url: this.$baseurl + '/prod-api/api/common/user',
							method: 'PUT',
							data:{
								phonenumber: this.form.phonenumber,
								nickName: this.form.userName
							},
							header:{Authorization: uni.getStorageSync('token')},
							success: (res) => {
								console.log(res)
								uni.showToast({
									title:'修改成功'
								})
								uni.switchTab({
									url:'/pages/user/user'
								})
							},
							fail: (err) => {
								uni.showToast({
									title:err.msg
								})
							}
						})
						this.disabled = true
					}else{
						console.log('验证失败')
					}
				})
			},
			getData(){
				uni.request({
					url:this.$baseurl + '/prod-api/api/common/user/getInfo',
					header: {Authorization: uni.getStorageSync('token')},
					success: (res) => {
						this.form = res.data.user
						if(res.data.user.sex === '0'){
							this.radio = '男'
						}else{
							this.radio = '女'
						}
					}
				})
			}
		},
		onReady() {
			this.$refs.uForm.setRules(this.rules)
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #F7F7F7;

		view {
			padding: 20rpx;

			.pannel {
				background-color: #fff;
				margin-top: 20rpx;

				.avatar {
					text-align: center;
				}

				.bottom {
					display: flex;
					flex-direction: row;
					justify-content: space-around;

					button:nth-child(1) {
						width: 200rpx;
						text-align: center;
						background-color: #ff9900;
						color: white;
					}

					button:nth-child(2) {
						width: 200rpx;
						text-align: center;
						background-color: #19b16b;
						color: white;
					}
				}
			}
		}
	}
</style>